package org.shj.netty.chartper2.aio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AsyncTimeServerHandler>{

	public void completed(AsynchronousSocketChannel result, AsyncTimeServerHandler attachment){
		System.out.println("AcceptCompletionHandler.completed()");
		attachment.getServerSocketChannel().accept(attachment, this);// 继续接收新的客户端请求
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		result.read(buffer,buffer, new ReadCompletionHandler(result));
		
		
	}

	@Override
	public void failed(Throwable exc, AsyncTimeServerHandler attachment) {
		System.out.println("AcceptCompletionHandler.failed()");
		exc.printStackTrace();
		
		attachment.latch.countDown();
	}
	
}
